<script setup>
import { orderListWithConditionService,changeStateService } from '@/api/order';
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
//导入token
import { userTokenStore } from '@/store/token'
const tokenStore = userTokenStore();
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { ElMessage, ElMessageBox } from 'element-plus'

//商品数据模型
const orders = ref([
    {
        "id": 1,
        "status": "0",
        //     -- 0为已处理 1为待处理 2为待接单 3为拒接
        "username": "molidis",
        "phone": "13344445555",
        "cartInfo": "EST-1",
        "date": "2025-3-11 16:39:16",
        "price": 168,
        "city": "CS"
    },
    {
        "id": 2,
        "status": "0",
        //     -- 0为已处理 1为待处理 2为待接单 3为拒接
        "username": "molidis",
        "phone": "13344445555",
        "cartInfo": "EST-1",
        "date": "2025-3-11 16:39:16",
        "price": 168,
        "city": "CS"
    },    
    {
        "id": 3,
        "status": "0",
        //     -- 0为已处理 1为待处理 2为待接单 3为拒接
        "username": "molidis",
        "phone": "13344445555",
        "cartInfo": "EST-1",
        "date": "2025-3-11 16:39:16",
        "price": 168,
        "city": "CS"
    }
])
//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(10)//每页条数

//用户搜索时选中的产品id
const orderIdForSearch = ref('')
//用户搜索时选中的商品状态
const phoneForSearch = ref('')

const hashmap = new Map([['0', '已处理'], ['1', '待处理'], ['2', '待接单'], ['3', '拒接']]);


//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size;
    getOrderWithCondition();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num;
    getOrderWithCondition();
}

//获取Item商品详情
const getOrderWithCondition = async () => {
    //处理数据
    let params = {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
        orderId: orderIdForSearch.value ? orderIdForSearch.value : null,
        phone: phoneForSearch.value ? phoneForSearch.value : null
    }
    let result = await orderListWithConditionService(params);
    if (result) {
        total.value = result.data.total;
        orders.value = result.data.list;
        console.log(orders);
    }
}
getOrderWithCondition();

//有条件搜索
const search = () => {
    getOrderWithCondition();
    ElMessage('获取成功');
}
//无条件搜索,用于重置
const searchWithNoCondition = () => {
    orderIdForSearch.value = null;
    phoneForSearch.value = null;
    getOrderWithCondition();
    ElMessage('获取成功');
}


const changeState = async (state,orderid) => {
    let result = await changeStateService(state,orderid);
    ElMessage(result.message ? result.message : '处理成功');
    getOrderWithCondition();
}
//删除商品弹窗
const stateMsg = ref('默认按钮');
const changeStateBox = (state,orderid) => {
    if(state === '2'){
        stateMsg.value='接单';
    }else if(state === '3'){
        stateMsg.value='拒接';
    }else if(state === '0'){
        stateMsg.value='处理接单';
    }
    ElMessageBox.confirm(
        '您确定要'+stateMsg.value+'吗',
        '注意',
        {
            confirmButtonText: stateMsg.value,
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            changeState(state,orderid);
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '删除取消',
            })
        })
}
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>订单管理</span>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="订单编号id：">
                <el-input v-model="orderIdForSearch" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
                <el-input v-model="phoneForSearch" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="search()">搜索</el-button>
                <el-button @click="searchWithNoCondition()">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- Item列表 -->
        <el-table :data="orders" style="width: 100%">
            <el-table-column label="订单编号id" prop="id" width="95"></el-table-column>
            <el-table-column label="状态" prop="status" width="68">
                <template #default="{ row }">
                    <!-- 使用 hashmap 获取对应的文本 -->
                    {{ hashmap.get(row.status) || '未知状态' }}
                </template>
            </el-table-column>
            <el-table-column label="用户名" prop="username"> </el-table-column>
            <el-table-column label="用户手机号" prop="phone" width="110"></el-table-column>
            <el-table-column label="购买商品" prop="cartInfo" width="150"></el-table-column>
            <el-table-column label="订单时间" prop="date" width="95"></el-table-column>
            <el-table-column label="总价" prop="price"></el-table-column>
            <el-table-column label="城市" prop="city"></el-table-column>
            <el-table-column label="操作" width="160">
                <template #default="{ row }">
                    <!-- //     -- 0为已处理 1为待处理 2为待接单 3为拒接-->
                    <el-button v-if="row.status === '1'" type="primary"
                        @click="changeStateBox('2',row.id)">
                        接单
                    </el-button>
                    <el-button v-if="row.status === '1'" type="primary"
                        @click="changeStateBox('3',row.id)">
                        拒接
                    </el-button>
                    <el-button v-if="row.status === '2'" type="primary"
                        @click="changeStateBox('0',row.id)">
                        处理接单
                    </el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}

/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}

.editor {
    width: 100%;

    :deep(.ql-editor) {
        min-height: 200px;
    }
}
</style>